/* Debug Funktionen */
var debug = {
	/* Objekt rekursiv ausgeben */		
	var_dump : function(obj, depth)
	{
		function create(obj, n, d)
		{
			var str = '';
			var s = '';
			var tab = 4;		
			n = !n? tab:n;
						
			function spc(i)
			{
				var s = '';
				for(var i=0;i<n;i++) s += '&nbsp;';
				
				return s;
			}
			
			function format(str, ofType)
			{
				var type = typeof(str);
				if(ofType && type != 'function')
					type = ofType;

				if(str === null)
					type = 'null';				
				
				var colors = {
					'string' : 'color: darkgreen',
					'number' : 'color: red',
					'boolean' : 'color: grey',
					'function' : 'color: darkblue',
					'prop' : 'color: blue',
					'type' : 'font-style: italic',
					'null' : 'font-weight: bold; color: darkblue'
				};

				str = '<span style="'+colors[type]+'">'+str+'</span>';
			
				if(type == 'function')
				{
					count++;
					var id = 'varDumpObj_'+count;
					str = '<span '+
						'onclick="window.opener.debug.var_dump_swap(\''+id+'\')" id="label_'+id+'" style="cursor:pointer">'+
						'[+]</span><br/>'+
						'<div style="display:none;width:100%;overflow:auto;border:1px solid darkgray;background:lightgrey" '+
						'id="'+id+'">'+str+'</div>';
				}			
			
				return str;
			}
			
			var s = spc(n);

			if(debug.in_array(typeof(obj), new Array('array', 'object')))
			{			
				for(prop in obj)
				{
					var o = debug.in_array(typeof(obj[prop]), new Array('array', 'object'))? true:false;
					var f = (typeof(obj[prop]) == 'function')? true:false;

					str += s+'['+format(prop, 'prop')+'] ('+format(typeof(obj[prop]), 'type')+')'+(o?br+s+'{'+br:' => ')+
						(o?((!depth || d < depth)?(noHTMLObj(obj[prop])?create(obj[prop], n+tab, d+1):s+
						spc(tab)+'['+format((obj[prop] !== null?'HTML Object '+(obj[prop].nodeName?obj[prop].nodeName:obj[prop]):null))+']'+br):s+spc(tab)+'['+format('...')+']'+
						br):format(stripHTML(obj[prop])))+(o?s+'}'+br:(f?'':br));	
				}					
			}
			else if(typeof(obj) == 'function')
				str += format(obj)+br;
			else
				str += obj+' ('+typeof(obj)+')';
				
			return str;
		}
		
		function noHTMLObj(obj)
		{
			if(obj)
			{
				if(obj.nodeName)
					return false;
				
				return true;
			}
			else
				return false;
		}
		
		function stripHTML(obj)
		{
			if(typeof(obj) == 'string')
			{
				obj = obj.replace(/</g, '&lt;').replace(/>/g, '&gt;');
			}
			
			return obj;
		}
				
		var br = '<br/>';
		depth = (depth === undefined)? false:depth;

		if(typeof(obj) != 'undefined')
		{
			var count = 0;
			var dump = create(obj, false, 1);
			dump = typeof(obj)+br+'{'+br+dump+'}';
		}
		else
			var dump = 'undefined object';

		this.var_dump_win = window.open('', 'var_dump_win', 'width=500,height=400,resizable=yes,scrollbars=yes');
		this.var_dump_win.focus();
		this.var_dump_win.document.body.innerHTML = '<pre>'+dump+'</pre>';
			
		return false;			
	},
	
	var_dump_win : new Object(),
	
	var_dump_swap : function(id)
	{
		var obj = this.var_dump_win.document.getElementById(id);
		var label = this.var_dump_win.document.getElementById('label_'+id);

		if(obj && label)		
		{
			var display = (obj.style.display == 'none')? false:true;
			obj.style.display = display? 'none':'block';
			label.innerHTML = display? '[+]':'[-]';
		} 
	},

	in_array : function(needle, haystack)
	{
		var n_empty = (needle.toString().length == 0)? true:false;
		var h_empty = (haystack.toString().length == 0)? true:false;
			
		if(n_empty || h_empty) return false;

		var operate = new Array();

		if(typeof(needle) == 'string')
			operate.push(needle);
		else
			operate = operate.concat(needle);

		for(var i=0;i<operate.length;i++)
		{
			var pattern = new RegExp(operate[i], 'g');
			var match = pattern.test(haystack);
				
			if(match) break;
		}

		return match;
	}
};
